package com.its.oa.system.permission.dao.impl;

import java.util.List;

import com.its.oa.dao.base.ItsHibernateDaoSupport;
import com.its.oa.model.Permission;
import com.its.oa.model.Role;
import com.its.oa.system.permission.dao.PermissionDao;
import com.its.oa.util.EntityValidata;

public class PermissionDaoImpl extends ItsHibernateDaoSupport implements
		PermissionDao {

	@SuppressWarnings("unchecked")
	public List<Permission> findPmsListByRoleList(List<Role> roleList) {
		String roleids = "";
		for (Role role : roleList) {
			roleids += EntityValidata.isNotEmpty(roleids)?",'"+role.getId()+"'":"'"+role.getId()+"'";
		}
		String hql ="select pr.permission from PermissionRole pr where pr.role.id in("+roleids+")";
		List<Permission> pmsList = getHibernateTemplate().find(hql);	
		return pmsList;
	}

	@SuppressWarnings("unchecked")
	public Permission findPermissionByUrl(String url) {
		String hql = "from Permission p where p.url like '"+url+"' ";
		List<Permission> list = getHibernateTemplate().find(hql);
		if(EntityValidata.isNotEmpty(list))
			return list.get(0);
		else
			return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Permission> findAll(){
		return getHibernateTemplate().find("from Permission");
	}
	
	@SuppressWarnings("unchecked")
	public List<Permission> findAllByPage(int offset,int pagesize) {
		String hql = "from Permission p order by p.module.id ";
		List<Permission> list = (List<Permission>) findByPage(hql, offset, pagesize);
		return list;
	}

	public void save(Permission pms) {
		getHibernateTemplate().save(pms);
	}

	public Permission findPermissionById(int pmsid) {
		return (Permission) getHibernateTemplate().get(Permission.class, pmsid);
	}
	
}
